// stores/index.js
import { defineStore } from 'pinia';
import { computed, ref,getCurrentInstance } from "vue";

export const useNavStore = defineStore('nav', ()=>{
	const {proxy} = getCurrentInstance()
	const statusBarHeights = ref()
	const top = ref()
	const Jnheight = ref()
	const tops = ref()
	const categoryid = ref('')
	const detail = ref({})
	// navbar
  const getNavBar =()=> {
	  const {safeArea,statusBarHeight,windowHeight} = uni.getSystemInfoSync();
	  const res = uni.getMenuButtonBoundingClientRect();
		statusBarHeights.value = res.top - statusBarHeight;
		top.value = safeArea.top
		Jnheight.value = res.height
		tops.value = top.value + statusBarHeights.value*2 + Jnheight.value
		console.log(statusBarHeights.value,top.value,Jnheight.value);
	}
	const carNum = ref(0)
	//购物车商品数量
	const getShopCar=async()=>{
		const result = await proxy.$api.gwcnums()
		if(result.code==200){
			console.log('购物车数量',carNum.value );
			carNum.value = result.data
		}
	}
	const orderNum = ref([])
	//订单数量装填
	const orderNoticeNum=async()=>{
		const result = await proxy.$api.orderNoticeNum()
		if(result.code==200){
			orderNum.value = result.data
		}
		if(result.data.length>0){
			var a =  String(result.data.length) 
			uni.setTabBarBadge({ //显示数字
				index: 2, //tabbar下标
				text:a//数字
			})
		}else{
			if(orderNum.value.length>0){
				uni.hideTabBarRedDot({ //隐藏红点
					index: 2 //tabbar下标
				})
			}
			
		}
		// setTimeout(()=>{
		// 	orderNoticeNum()
		// },5000)
	}
	const address = ref('')
	//获取地址
	const getLocation=()=> {
		console.log('getLocation');
		uni.getLocation({
			type: 'wgs84',
			isHighAccuracy: true,
			success: (res) => {
				console.log('getLocation',res);
				uni.setStorageSync('location', `${res.latitude},${res.longitude}`)
				uni.request({
					url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
					data: {
						key: '1d57235734cdaf8f4e022bc2b5cfb312',
						location: `${res.longitude},${res.latitude},`
					}
				}).then(res => {
					address.value =  res.data.regeocode.addressComponent.city
					console.log('address.value',address.value);
				})
			},
			fail:(err)=>{
				console.log('getLocation',err);
				uni.getSetting({
					success:function(res){
						var statu = res.authSetting;
						console.log('statu',statu);
						if (!statu['scope.userLocation']) {
								console.log('地理权限没有');
								uni.showModal({
									title: '是否授权当前位置',
									content: '需要获取您的地理位置，请确认授权，否则地理功能将无法使用',
									success(tip) {
										if(tip.confirm){
											uni.openSetting({
												success:function(data){
													if (data.authSetting["scope.userLocation"] === true) {
															uni.showToast({
																title: '授权成功',
																icon: 'success',
																duration: 1000
															})
															setTimeout(()=>{
																uni.getLocation({
																	type: 'wgs84',
																	isHighAccuracy: true,
																	success: (res) => {
																		console.log('getLocation',res);
																		uni.setStorageSync('location', `${res.latitude},${res.longitude}`)
																		uni.request({
																			url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
																			data: {
																				key: '1d57235734cdaf8f4e022bc2b5cfb312',
																				location: `${res.longitude},${res.latitude},`
																			}
																		}).then(res => {
																			address.value =  res.data.regeocode.addressComponent.city
																			console.log('address.value',address.value);
																		})
																	},
											          	})
															},1000)
													}
												
												}
											})
										}else{
											uni.showToast({
												title: '授权失败',
												icon: 'none',
												duration: 1000
											})
										}		
									
									}
							  })
						}
					}
				})
			}
			
		});
	}
	return{
	statusBarHeights,top,detail,
	Jnheight,tops,getNavBar,getLocation,
	address,getShopCar,carNum,orderNoticeNum,orderNum
	}
});
